<template>
  <div style="padding: 40px">
    <!-- 新增弹窗 -->

    <el-form :model="form" label-width="120px" :disabled="dialogVisible">
      <el-form-item label="账号">
        <el-input v-model="form.userAccount" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="姓名">
        <el-input v-model="form.username" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="地址">
        <el-input v-model="form.address" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="电话">
        <el-input v-model="form.telephone" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="简介">
        <el-input v-model="form.userProfile" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="密码">
        <el-input v-model="form.userPassword" style="width: 50%"></el-input>
      </el-form-item>
      <el-form-item label="上传头像">
        <el-upload
            action="#"
            :file-list="fileList"
            list-type="picture-card"
            :limit="1"
            :on-change="handelChange"
            :data="dataList"
            :auto-upload="false">
          <i slot="default" class="el-icon-plus"></i>
        </el-upload>
      </el-form-item>
    </el-form>
    <span class="dialog-footer">
          <el-button @click="dialogVisible = false">取消</el-button>
          <el-button type="primary" @click="saveEdit">{{dialogVisible?'修改':'保存'}}</el-button>
        </span>
  </div>
</template>

<script>
import { mapGetters } from 'vuex'
import {getUserUpdateMy, getUserId, getGuanliyuanId, getGuanliyuanUpdateMy, getThreeDuaId} from '../../http/user.js'
import { getMinioUpload } from '../../http/minio.js'
export default {
  name: "Notice",

  data() {
    return {
      form: {
         "userAccount":""
      },
      fileList:[],
      dataList: {},
      dialogVisible: true,
      currentPage: 1,
      pageSize: 5,
      total: '',
      tableData: [],
    }

  },
  created() {
    this.load()
  },

  methods: {
    async  handelChange(file, fileList){
      let formData =  new FormData()
      formData.append("data", file.raw);
      const res = await  getMinioUpload(formData)
      this.fileList =  [{name: 'food.jpg', url: res}]
    },
    async load() {
      const { code,  data } = await getThreeDuaId({ id : this.userId})
      if (code == '0') {
        this.form = data
        this.fileList =  [{name: 'food.jpg', url: data.userAvatar}]
      }
    },
    // 确认修改
    async saveEdit() {

      if (this.dialogVisible) {
        this.dialogVisible = !this.dialogVisible
      }else{
        this.form.userAvatar = this.fileList[0].url

        const { code, message } = await getGuanliyuanUpdateMy(this.form)
        if (code == '0') {
          this.$message({
            type: "success",
            message: "修改成功"
          });

        } else {
          this.$message({
            type: "error",
            message: "修改失败"
          })
        }
        this.dialogVisible = true
        this.load()
      }

    },
  },
  computed: {
    ...mapGetters({
      userId: 'user/getUserId',
      userRole: 'user/getUserRole'
    })
  }

}
</script>


<style scoped>
.dialog-footer{
  display: flex;
  justify-content: end;
}
</style>
